import Ele from '../ele/Ele'
import ForEle from '../ele/ForEle'
import AttrEle from '../ele/AttrEle'
import TextEle from '../ele/TextEle'
import FormEle from '../ele/FormEle'
import IfEle from '../ele/IfEle'
import CompEle from '../ele/CompEle'
import Builder from './Builder'
import RouterEle from '../ele/RouterEle'
import Okay from '../Okay'
export default class EleTreeBuilder {
    constructor() {
        this.extends = new Set()
    }
    push_builder (builder) {
        if (!(builder instanceof Builder)) {
            throw '参数类型错误：非Builder类'
        }
        this.extends.add(builder)
    }
    build(ele, okay) {
        let childs = ele.ele.childNodes
        for (let child of childs) {
            if (child.nodeType == 3) {
                let s = child.nodeValue.trim()
                if (s == '') {
                    continue
                }
                if(/^[\s\S]*\{\{[^\}]+\}\}[\s\S]*/.test(s)){
                    ele.append(new TextEle(okay, child))
                }
            }else if (child.tagName == 'ROUTE') {
                ele.append(new RouterEle(okay, child))
            }else if (child.tagName == 'FOR') {
                ele.append(new ForEle(okay, child))
            }else if (child.tagName == 'IF') {
                let if_ele = new IfEle(okay, child)
                ele.append(if_ele)
                this.build(if_ele, okay)
            }else if (child.getAttribute('model')) {
                let fele = new FormEle(okay, child)
                ele.append(fele)
                if (child.tagName == 'SELECT') {
                    this.build(fele, okay)
                }
            }else if(okay.comps.has(child.tagName.toLowerCase()) || Okay.components.has(child.tagName.toLowerCase())) {
                ele.append(new CompEle(child.tagName.toLowerCase(), okay, child))
            }else {
                let s = child.outerHTML;
                let re = new RegExp(/^<[a-zA-Z0-9]+\s+[^>]+\{\{[^\}{2}]+\}\}[^>]*>/);
                if(re.test(s)){
                    let ae = new AttrEle(okay, child);
                    ele.append(ae);
                    this.build(ae,okay);
                }else{
                    if (this.extends.size > 0) {
                        let is_judge = false
                        for (let extend of this.extends) {
                            if (extend.judge(child, okay)) {
                                let el = extend.get_ele(child, okay)
                                ele.append(el)
                                if (extend.is_box()) {
                                    this.build(el, okay)
                                }
                                is_judge = true
                                break;
                            }
                        }
                        if (!is_judge) {
                           let el = new Ele(okay, child)
                           ele.append(el)
                           this.build(el, okay)
                        }
                    }else {
                        let e1 = new Ele(okay, child);
                        ele.append(e1);
                        this.build(e1,okay);
                    }

                }
            }
        }
    }
}
